import TCPServer from "../middleware/TCPServer";
import shell = require("shelljs");
/**
 *  任务服务器, 接受web服务器的网络请求
 *  处理一些比较耗时的任务
 *  比如编译项目，解压文件等
 */

 export default class TaskServer{
     private tcpServer?:TCPServer;
     public startListen():void{
        if(this.tcpServer){
            throw new Error("请不要多次启动服务器");
            return;
        } 
        this.tcpServer = new TCPServer("TaskServer", 5701);

        this.tcpServer.start((data:string)=>{
            try{
                let command = JSON.parse(data);
                let filePath = command["file"];
                let dir = command["dir"];
                shell.exec("unzip -o "+filePath + " -d "+dir);
            }
            catch(ex){
                console.log("收到命令转JSON出错， ", data);
            }
            
         });
     }
     public sendToClient(jsonString:string){
        if(!this.tcpServer){
            return;
        }
        this.tcpServer.sendToAll(jsonString);
     }
 }
